﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IServices.ProcessExamine;
using ProTrack.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 检验单据
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class QcProcessRecordController : ControllerBase
    {
        public QcProcessRecordController(IQcProcessRecordServices services, LogHelper LogHelper)
        {
            Services = services;
            this.LogHelper = LogHelper;
        }

        public IQcProcessRecordServices Services { get; }
        public LogHelper LogHelper { get; }





        /// <summary>
        /// 获取所有检验单据
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await Services.GetAllAsync();
                status.Data = list;
                status.Count = list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("检验单据-获取所有", ex, HttpContext);
            }


            return Ok(status);
        }




        /// <summary>
        /// 分页获取检验单据
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="RecordCode">工序检验单编号</param>
        /// <param name="ProcessCode">工序编号</param>
        /// <param name="ReportUser">报工人</param>
        /// <returns></returns>

        [HttpGet]
        public async Task<IActionResult> Get([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string RecordCode, [FromQuery] string ProcessCode, [FromQuery] string ReportUser)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                RecordCode = RecordCode ?? "";
                ProcessCode = ProcessCode ?? "";
                ReportUser = ReportUser ?? "";

                var list = await Services.GetAsync(PageIndex, PageSize, RecordCode, ProcessCode, ReportUser);
                status.Data = list;
                status.Count = await Services.GetCountAsync(RecordCode, ProcessCode, ReportUser);
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("检验单据-分页获取", ex, HttpContext);
            }

            return Ok(status);
        }



        /// <summary>
        /// 新增检验单据
        /// </summary>
        /// <param name="model">检验单据模型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] QcProcessRecord model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                QcProcessRecordItem mdoel2 = new QcProcessRecordItem() {
                    ProductCode=model.ProductCode,
                    Flag="0",
                    CreateTime= StringHelper.GetTimeNow(),
                    Creator= User.Identity.Name,

                };

                QcProcessRecordResult mdoel3 = new QcProcessRecordResult() {
                    ProductCode = model.ProductCode,
                    ProcessCode= model.ProcessCode,
                    ProcessName=model.ProcessName,
                    DetectionNumber=model.Quantity,
                    InspectFlag=model.InspectFlag,
                    CreateTime = StringHelper.GetTimeNow(),
                    Creator = User.Identity.Name,
                };
                model.CreateTime = StringHelper.GetTimeNow();
                model.Creator = User.Identity.Name;


                var result = await Services.AddAsync(model, mdoel2, mdoel3);
                if (result)
                {
                    status.Msg = "新增成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "新增失败,请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("检验单据-新增", ex, HttpContext);
            }
            return Ok(status);


        }


        /// <summary>
        /// 修改检验单据
        /// </summary>
        /// <param name="model">单位模型</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromForm] QcProcessRecord model)
        {
            var status = BaseStatus.CreateInstance();
            List<QcProcessStandardItem> list = new List<QcProcessStandardItem>();
            try
            {
                QcProcessRecordItem mdoel2 = new QcProcessRecordItem()
                {
                    ProductCode = model.ProductCode,
                    Flag = "0",
                    UpdateTime = StringHelper.GetTimeNow(),
                    Updater = User.Identity.Name,

                };

                QcProcessRecordResult mdoel3 = new QcProcessRecordResult()
                {
                    ProductCode = model.ProductCode,
                    ProcessCode = model.ProcessCode,
                    ProcessName = model.ProcessName,
                    DetectionNumber = model.Quantity,
                    InspectFlag = model.InspectFlag,
                    UpdateTime = StringHelper.GetTimeNow(),
                    Updater = User.Identity.Name,
                };



                model.UpdateTime = StringHelper.GetTimeNow();
                model.Updater = User.Identity.Name;

                var isSuccess = await Services.UpdateAsync(model, mdoel2, mdoel3);
                if (isSuccess)
                {
                    status.Msg = "修改成功";

                    //操作日志
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "修改失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("检验单据-修改", ex, HttpContext);
            }

            return Ok(status);
        }




        /// <summary>
        /// 批量删除检验单据
        /// </summary>
        /// <param name="Ids">Id数组</param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll([FromForm] string Ids)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var arr = Ids.SplitId();
                var isSuccess = await Services.DeleteAsync(arr);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("检验单据-批量删除", ex, HttpContext);
            }

            return Ok(status);
        }



        /// <summary>
        /// 删除检验单据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]

        public async Task<IActionResult> Del([FromForm] int Id)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var isSuccess = await Services.DeleteAsync(Id);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("检验单据-删除", ex, HttpContext);
            }

            return Ok(status);
        }









    }
}
